//
//  DoorAlert.m
//  RONGAO
//
//  Created by wei wang on 2017/3/24.
//  Copyright © 2017年 wei wang. All rights reserved.
//

#import "DoorAlert.h"
@interface DoorAlert()
@property (nonatomic,strong)UIView *containerView;
@property (nonatomic,strong)UIView *topView;
/** textfile **/
@property(nonatomic,strong) UITextField *pwdFiled;
@end

@implementation DoorAlert
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self configView];
        
    }
    return self;
}


- (instancetype)init{
    self = [super init];
    if (self) {
        
        [self configView];
        
    }
    
    return self;
}


- (void)configView{
    
    
    
    __weak typeof(self) weakSelf = self;
    [self.containerView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.width.mas_equalTo(pxScaleWidth(750));
        make.centerX.equalTo(weakSelf.mas_centerX);
        make.centerY.mas_equalTo(weakSelf.mas_centerY);
        make.height.mas_equalTo(200);
    }];
    
    
    
    [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(weakSelf.containerView);
        make.top.equalTo(weakSelf.containerView);
        make.right.equalTo(weakSelf.containerView);
        make.height.mas_equalTo(50);
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(weakSelf.topView);
        make.top.equalTo(weakSelf.topView);
        make.right.equalTo(weakSelf.topView);
        make.height.equalTo(weakSelf.topView);
    }];
    
    [self.pwdFiled mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.mas_equalTo(weakSelf.containerView.mas_centerX);
        make.centerY.mas_equalTo(weakSelf.containerView.mas_centerY);
        make.left.equalTo(weakSelf.containerView.mas_left).mas_offset(20);
        make.right.equalTo(weakSelf.containerView.mas_right).mas_offset(-20);
        make.height.mas_equalTo(40);
    }];
    
    [self.sureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.equalTo(weakSelf.containerView.mas_right);
        make.bottom.equalTo(weakSelf.containerView.mas_bottom);
        make.height.mas_equalTo(50);
        make.width.equalTo(weakSelf.cancelBtn);
        
    }];
    
    
    [self.cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(weakSelf.sureBtn.mas_left);
        make.left.equalTo(weakSelf.containerView.mas_left);
        make.bottom.equalTo(weakSelf.containerView.mas_bottom);
        make.height.equalTo(weakSelf.sureBtn);
    }];
    
}

#pragma makr - 点击事件
- (void)sureClick:(UIButton *)sender{
    
    if ([self.pwdFiled.text length] >= 4) {
        [self endEditing:YES];
        NSLog(@"获取到的密码是：%@",self.pwdFiled.text);
        __weak typeof(self) weakself = self;
        if (self.pressSureBtn) {
            
            weakself.pressSureBtn(weakself.pwdFiled.text);
        }
    }else{
        
        NSLog(@"密码不足4位数");
    
        [self outWithAnimation];
    }
    
}


- (void)cancelClick:(UIButton*)sender{
    
    NSLog(@"点击了取消");
    [self endEditing:YES];
    if (self.pressCancelBtn) {
        self.pressCancelBtn();
    }
}

#pragma mark - 动画
- (void)outWithAnimation{
    
    typeof (self)weakSelf = self;
    [UIView animateWithDuration:.35f animations:^{
        CGRect frame =  weakSelf.frame;
        frame.origin.y = weakSelf.frame.size.height;
        weakSelf.frame = frame;
        
    } completion:^(BOOL finished) {
        if (finished) {
            [weakSelf removeFromSuperview];
        }
    }];
    
}

#pragma mark - 懒加载
- (UIView *)containerView{
    
    if (!_containerView) {
        _containerView = [[UIView alloc]init];
        _containerView.backgroundColor = [UIColor whiteColor];
        [self addSubview:self.containerView];
        self.containerView.layer.masksToBounds = YES;
        self.containerView.layer.cornerRadius = 10.0f;
    }
    return _containerView;
}

- (UIView *)topView{
    if (!_topView) {
        _topView = [[UIView alloc]init];
        _topView.backgroundColor = [UIColor colorWithRed:217/255.0 green:183/255.0 blue:108/255.0 alpha:1.0f];
        [self.containerView addSubview:self.topView];
    }
    return _topView;
}

- (UILabel *)titleLabel{
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc]init];;
        _titleLabel.textColor = [UIColor whiteColor];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        _titleLabel.font = [UIFont systemFontOfSize:17];
        [self.topView addSubview:self.titleLabel];
    }
    return _titleLabel;
}

- (UITextField *)pwdFiled{
    if (!_pwdFiled) {
        _pwdFiled = [[UITextField alloc]init];
        [self.containerView addSubview:_pwdFiled];
        _pwdFiled.borderStyle = UITextBorderStyleRoundedRect;
        _pwdFiled.keyboardType = UIKeyboardTypeNumberPad;
        [_pwdFiled setSecureTextEntry:YES];
    }
    return _pwdFiled;
}

- (UIButton *)sureBtn{
    if (!_sureBtn) {
        _sureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_sureBtn setTitle:@"确定" forState:UIControlStateNormal];
        [_sureBtn setTitleColor:[UIColor colorWithRed:217/255.0 green:183/255.0 blue:108/255.0 alpha:1.0f] forState:UIControlStateNormal];
        [_sureBtn addTarget:self action:@selector(sureClick:) forControlEvents:UIControlEventTouchUpInside];
        _sureBtn.layer.borderWidth = .3f;
        _sureBtn.layer.borderColor = [UIColor groupTableViewBackgroundColor].CGColor;
        [self.containerView addSubview:_sureBtn];
        
    }
    return _sureBtn;
}

- (UIButton *)cancelBtn
{
    if (!_cancelBtn) {
        _cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
        [_cancelBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        _cancelBtn.layer.borderWidth = .3f;
        _cancelBtn.layer.borderColor = [UIColor groupTableViewBackgroundColor].CGColor;
        [_cancelBtn addTarget:self action:@selector(cancelClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.containerView addSubview:_cancelBtn];
    }
    return _cancelBtn;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
